package com.eunion.core.common.request;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import javax.validation.Valid;

import org.springframework.data.domain.Sort;

import com.eunion.core.common.request.impl.SortingRequestImpl;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, defaultImpl = SortingRequestImpl.class)
public interface SortingRequest<T> extends Serializable {

    @Valid
    T getQuery();

    List<Sorting> getSortings();

    default Sort toSort() {
        return Optional.ofNullable(getSortings()).orElse(Collections.emptyList()).stream().map(s -> {
            return new Sort(s.toOrder());
        }).reduce(Sort::and).orElse(null);
    }

}
